<?php
/**
 * @ClassName: Mall
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-10 14:24
 * @Version: V1.0.0
 */

namespace app\api\controller\points;

use app\api\controller\Api;
use app\common\model;

class Mall extends Api
{
    /**
     * 积分商品分类
     * @return \think\response\Json
     */
    function categoryList()
    {
        $role = input('role', 0);
        $user_type = input('user_type', 0);
        if ($role == 1) {//商家
            if ($user_type == 1) {//联盟
                $list = model\PointsProductCategory::where('flag', 'union')->where('is_show', 1)->order('sort')->select();
            } else {//水站
                $list = model\PointsProductCategory::where('flag', 'water')->where('is_show', 1)->order('sort')->select();
            }
        } elseif ($role == 3) {//代理
            $list = model\PointsProductCategory::where('flag', 'agent')->where('is_show', 1)->order('sort')->select();
        } else {//其他
            $list = [];
        }

        $result = [];
        foreach ($list as $item) {
            $result[] = [
                'id' => $item['id'],
                'name' => $item['name'],
            ];
        }

        return json(['code' => 1, 'data' => $result]);
    }

    /**
     * 积分商品
     * @return \think\response\Json
     */
    function goodsList()
    {
        $shop_type = input('user_type', 0);
        $category_id = input('category_id', 0);
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $where = [];
        $where[] = ['shop_type', '=', $shop_type];
        $where[] = ['on_sale', '=', 1];
        $where[] = ['is_show', '=', 1];

        if ($category_id) {
            $where[] = ['category_id', '=', $category_id];
        }

        $list = model\PointsProduct::where($where)->order('sales_sum desc')->limit($skip, $limit)->select();

        foreach ($list as &$item) {
            $item['logo'] = replace_url($item['logo']);
            $item['imagesList'] = replace_many_url($item['images']);
            $item['content'] = replace_src_url($item['content']);
        }

        $result = [
            'goodsList' => $list,
            'is_more' => count($list) >= $limit,
        ];

        return json(["code" => 1, "data" => $result]);
    }

}